package com.h4313.ghome.ui;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;

import com.google.android.gcm.GCMRegistrar;
import com.h4313.ghome.GHomeApplication;
import com.h4313.ghome.R;
import com.h4313.ghome.io.ServerUtilities;
import com.h4313.ghome.ui.fragments.Refreshable;
import com.h4313.ghome.ui.fragments.TemperatureFragment;

public class HomeActivity extends Activity {

	// Refreshable mTemperature;
	Refreshable mMorningConf, mTemperature;
	private AsyncTask<Void, Void, Void> mRegisterTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (GHomeApplication.DEBUG_MODE) {
			// GCMRegistrar.checkDevice(this);
			// GCMRegistrar.checkManifest(this);
		}

		setContentView(R.layout.activity_home);

		final String regId = GCMRegistrar.getRegistrationId(this);
		if (regId.equals("")) {
			GCMRegistrar.register(this, GHomeApplication.SENDER_ID);
		} else {
			if (GCMRegistrar.isRegisteredOnServer(this)) {
				// Skips registration.
			} else {
				// Try to register again, but not in the UI thread.
				// It's also necessary to cancel the thread onDestroy(),
				// hence the use of AsyncTask instead of a raw thread.
				final Context context = this;
				mRegisterTask = new AsyncTask<Void, Void, Void>() {

					@Override
					protected Void doInBackground(Void... params) {
						boolean registered = ServerUtilities.register(context,
								regId);
						// At this point all attempts to register with the app
						// server failed, so we need to unregister the device
						// from GCM - the app will try to register again when
						// it is restarted. Note that GCM will send an
						// unregistered callback upon completion, but
						// GCMIntentService.onUnregistered() will ignore it.
						if (!registered) {
							GCMRegistrar.unregister(context);
						}
						return null;
					}

					@Override
					protected void onPostExecute(Void result) {
						mRegisterTask = null;
					}

				};
				mRegisterTask.execute(null, null, null);
			}
		}

		mTemperature = (Refreshable) getFragmentManager().findFragmentById(
				R.id.frag_temperature);
		final FragmentManager mManager = getFragmentManager();
		mMorningConf = (Refreshable) mManager
				.findFragmentById(R.id.frag_morning_conf);
	}

	@Override
	protected void onStart() {
		super.onStart();

		refreshAll();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_home, menu);
		// keyboard(s) are displayed only when we click on the edit text
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.menu_refresh) {
			refreshAll();
			return true;
		} else {
			return super.onOptionsItemSelected(item);
		}
	}

	private void refreshAll() {
		mTemperature.refresh();
		mMorningConf.refresh();
	}

	@Override
	protected void onDestroy() {
		if (mRegisterTask != null) {
			mRegisterTask.cancel(true);
		}
		// GCMRegistrar.onDestroy(this);
		super.onDestroy();
	}
}
